package form.ui.core {
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import form.ui.Label;
	
	public class Container extends FMSprite 
	{
		public var labelTop:Number = 0;
		public var labelRight:Number = 0;
		public var labelLeft:Number = 0;
		
		protected var labelField:Label;
		
		
		public function Container() {
			labelField = new Label();
			super();
			
			addChild(labelField);
			addEventListener(Event.ADDED_TO_STAGE, onAddToStage, false, 0, true);
		}
		
		public function set label(value:String):void
		{
			labelField.formValue = value;
		}
		public function get label():String
		{
			return labelField.formValue;
		}
		
		
		public function get labelAlign():String
		{
			return labelField.textAlign;
		}
		public function set labelAlign(value:String):void
		{
			labelField.textAlign = value;
		}
		public function get labelColor():uint
		{
			return labelField.fontColor;
		}
		public function set labelColor(value:uint):void
		{
			labelField.fontColor = value;
		}
		
		public override function addChild(child:DisplayObject):DisplayObject {
			return this.addChildAt(child, this.numChildren);
		}
		
		public override function addChildAt(child:DisplayObject, index:int):DisplayObject {
			var added:DisplayObject = super.addChildAt(child, index);
			
			this.position();
			
			return added;
		}
		
		public override function removeChild(child:DisplayObject):DisplayObject {
			var removed:DisplayObject = super.removeChild(child);
			
			this.position();
			
			return removed;
		}
		
		public override function removeChildAt(index:int):DisplayObject {
			return this.removeChild(this.getChildAt(index));
		}
		
		public function set synchronizeWidth(width:int):void {
			var numChildren:int = this.numChildren;
			for (var i:int = 0; i < numChildren; i++) {	
				this.getChildAt(i).width = width;
			}
			
			this.width = width;
		}
		
		public function set synchronizeHeight(height:int):void {
			var numChildren:int = this.numChildren;
			for (var i:int = 0; i < numChildren; i++) {	
				this.getChildAt(i).height = height;
			}
			
			this.height = height;
		}
		
		protected function position():void {
			var numChildren:int = this.numChildren;
			var width:int = 0;
			var height:int = 0;
			
			for (var i:int = 0; i < numChildren; i++) {
				var child:DisplayObject = this.getChildAt(i);
				
				child.x = 0;
				child.y = 0;
				
				if (width < child.width) {
					width = child.width;
				}
				if (height < child.height) {
					height = child.height;
				}
			}
			
			this.dimensions = [width, height];
		}
		
		override public function draw():void
		{
			super.draw();
			if(labelLeft != 0)labelField.x = labelLeft;				
			if(contains(labelField))labelField.y = (this.height ? this.height / 2 - labelField.height / 2 : 0) + labelTop;
		}
		
		override public function set width(value:Number):void
		{
			if(contains(labelField))labelField.width = value - labelLeft;			
			super.width = value;
		}
		
		override public function get width():Number
		{
			if(contains(labelField))return super.width + labelField.width + labelLeft;
			else return super.width;
		}
		
		private function onAddToStage(e:Event):void
		{
			if(labelField && contains(labelField)) setChildIndex(labelField, numChildren - 1);
		}
	}
}